﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IDAL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.DAL
{
    public class MasEducationDegreeDAL : IMasEducationDegreeDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public MasEducationDegreeDAL()
        {

        }
        public DataSet LoadEducationDegree(Nullable<int> Sequence)
        {
            string strSQL;

            DataSet ds = null;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Sequence", Sequence);

                strSQL = "SELECT EDU_DEGREE_SEQ, EDU_DEGREE_CODE, EDU_DEGREE_NAME, EDU_DEGREE_NAME_ENG"
                        + ", EDU_DEGREE_QUALIFICATION, COMPARATIVE_LEVEL, RECORD_STATUS, CREATED_BY, UPDATED_BY"
                    + " FROM MAS_EDU_DEGREES"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + (Sequence.HasValue ? " AND EDU_DEGREE_SEQ = @Sequence" : "");

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return ds;
        }
        public IList<MasEducationDegreeInfo> ListEducationDegree(MasEducationDegreeInfo degreeInfo)
        {
            string strSQL;

            List<MasEducationDegreeInfo> infoList = new List<MasEducationDegreeInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Seq", degreeInfo.Sequence);

                strSQL = "SELECT EDU_DEGREE_SEQ, EDU_DEGREE_CODE, EDU_DEGREE_NAME, EDU_DEGREE_NAME_ENG"
                        + ", EDU_DEGREE_QUALIFICATION, COMPARATIVE_LEVEL, RECORD_STATUS, CREATED_BY, UPDATED_BY"
                    + " FROM MAS_EDU_DEGREES"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + (degreeInfo.Sequence > 0 ? " AND EDU_DEGREE_SEQ = @Seq" : "");

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    MasEducationDegreeInfo Info = new MasEducationDegreeInfo(rdr.IsDBNull(0) ? (int?)null : rdr.GetInt32(0)
                            , rdr.IsDBNull(1) ? "" : rdr.GetString(1)
                            , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                            , rdr.IsDBNull(3) ? "" : rdr.GetString(3)
                            , rdr.IsDBNull(4) ? "" : rdr.GetString(4)
                            , rdr.GetInt32(5)
                            , rdr.IsDBNull(6) ? "" : rdr.GetString(6)
                            , rdr.IsDBNull(7) ? (int?)null : rdr.GetInt32(7)
                            , rdr.IsDBNull(8) ? (int?)null : rdr.GetInt32(8));
                    infoList.Add(Info);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return infoList;
        }

        public void Insert(MasEducationDegreeInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@Code", Info.Code);
                dbManager.AddParameters(1, "@Name", Info.Name);
                dbManager.AddParameters(2, "@NameEng", Info.NameEng);
                dbManager.AddParameters(3, "@Qualification", Info.Qualification);
                dbManager.AddParameters(4, "@ComparativeLevel", Info.ComparativeLevel);
                dbManager.AddParameters(5, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(6, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(7, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO MAS_EDU_DEGREES(EDU_DEGREE_CODE, EDU_DEGREE_NAME, EDU_DEGREE_NAME_ENG, EDU_DEGREE_QUALIFICATION"
                        + ", COMPARATIVE_LEVEL, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                     + " VALUES(@Code, @Name, @NameEng, @Qualification, @ComparativeLevel"
                        + ", @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Update(MasEducationDegreeInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(9);

                dbManager.AddParameters(0, "@Sequence", Info.Sequence);
                dbManager.AddParameters(1, "@Code", Info.Code);
                dbManager.AddParameters(2, "@Name", Info.Name);
                dbManager.AddParameters(3, "@NameEng", Info.NameEng);
                dbManager.AddParameters(4, "@Qualification", Info.Qualification);
                dbManager.AddParameters(5, "@ComparativeLevel", Info.ComparativeLevel);
                dbManager.AddParameters(6, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(7, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(8, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE MAS_EDU_DEGREES"
                        + " SET EDU_DEGREE_CODE = @Code"
                            + ", EDU_DEGREE_NAME = @Name"
                            + ", EDU_DEGREE_NAME_ENG = @NameEng"
                            + ", EDU_DEGREE_QUALIFICATION = @Qualification"
                            + ", COMPARATIVE_LEVEL = @ComparativeLevel"
                            + ", UPDATED_BY = @UpdatedBy"
                            + ", UPDATED_WHEN = @UpdatedWhen"
                     + " WHERE EDU_DEGREE_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE MAS_EDU_DEGREES"
                        + " SET RECORD_STATUS = @RecordStatus"
                            + ", UPDATED_BY = @UpdatedBy"
                            + ", UPDATED_WHEN = @UpdatedWhen"
                     + " WHERE EDU_DEGREE_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
